<template>
  <!-- 角色表单弹窗 -->
  <Modal :modelValue="modelValue" :title="title" @close="handleCloseDialog" @open="handleOpenDialog" maxHeight="auto">
    <el-form ref="roleFormRef" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入角色名称" />
      </el-form-item>

      <el-form-item label="角色编码" prop="role_code">
        <el-input v-model="form.role_code" placeholder="请输入角色编码" />
      </el-form-item>

      <el-form-item label="数据范围" prop="data_scope">
        <el-select v-model="form.data_scope">
          <el-option :key="0" label="全部" value="全部" />
          <el-option :key="1" label="本级" value="本级" />
          <el-option :key="2" label="自定义" value="自定义" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.data_scope === '自定义'" label="数据权限" prop="depts">
        <el-select v-model="form.data_scope">
          <el-option :key="0" label="全部" value="全部" />
          <el-option :key="1" label="本级" value="本级" />
          <el-option :key="2" label="自定义" value="自定义" />
        </el-select>
      </el-form-item>
      <el-form-item label="角色级别" prop="level">
        <el-input-number v-model="form.level" controls-position="right" :min="0" style="width: 100px" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="modal-footer">
        <el-button type="primary" @click="handleSubmit" :loading="loading" :disabled="loading">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
      </div>
    </template>
  </Modal>
</template>

<script setup lang="ts">
import RoleAPI, { RoleForm, RoleVO } from '@/api/role';

// 定义接收的属性
interface Props {
  modelValue: boolean
  selectedItem?: RoleVO
  title?: string
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  title: '表单',
})

// 自定义事件
const emit = defineEmits<{
  submit: [formData: RoleForm]
  close: []
}>()
const roleFormRef = ref(ElForm)
const loading = ref(false)

const form = ref<RoleForm>({})

const rules = reactive({
  name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
  role_code: [{ required: true, message: "请输入角色编码", trigger: "blur" }],
  data_scope: [{ required: true, message: "请选择数据权限", trigger: "blur" }],
  level: [{ required: true, message: "请选择级别", trigger: "blur" }],
})

//获取表单数据
const getFormData = (roleId: number) => {
  // 调用接口获取角色信息
  RoleAPI.getFormData(roleId).then((res) => {
    if (res.status === 200) {
      Object.assign(form.value, res.data)
    }
  })
}

const handleOpenDialog = () => {

}
const handleCloseDialog = () => {
  resetForm()
  emit('close')
}

const submitClick = (data: any) => {
  resetForm()
  emit('submit', data)
}

const resetForm = () => {
  if (roleFormRef.value) {
    roleFormRef.value.resetFields()
    roleFormRef.value.clearValidate()
  }
  form.value = {}
}

const handleSubmit = () => {
  roleFormRef.value.validate((valid: any) => {
    if (valid) {
      if (form.value.role_id) {
        updateData()
      } else {
        saveData()
      }
    }
  })
}

const saveData = () => {
  loading.value = true
  RoleAPI.add(form.value).then((res) => {
    if (res.status === 200) {
      ElMessage.success("新增成功")
      submitClick(res.data)
    }
  }).finally(() => {
    loading.value = false
  })
}
const updateData = () => {
  loading.value = true
  RoleAPI.update(form.value).then((res) => {
    if (res.status === 200) {
      ElMessage.success("修改成功")
      submitClick(res.data)
    }
  }).finally(() => {
    loading.value = false
  })
}


//表单传递数据
watch(() => props.selectedItem, (newVal, oldVal) => {
  if (newVal && newVal.role_id) {
    getFormData(newVal.role_id)
  } else {
    form.value = Object.assign({}, newVal)
  }
}, { immediate: true, deep: true })


</script>
